$(function(){
  var user={
     init:function(){
    	 var _this=this;
    	 _this.initDatagrid();
    	 _this.initEvent();
     },
     initEvent:function(){
	    var _this=this;
 		//添加实现
 		$("#add_user").click(function(){
 	        top.$.modalDialog({
                _form:"#userForm", 
                _datagrid:_this.userDataGrid,
 	            title : '用户信息添加',
 	            href:"/sys_user/add",
 	            onLoad:function(){
 	            	var f=top.$.modalDialog.handler.find("#userForm"); 
 	            	f.form("reset");
 	            }
 	        }).dialog('open');
 		});
 		//删除实现
 		$("#del_user").click(function(){
 			var selRows=_this.userDataGrid.datagrid('getSelections');
 			JSmile.KEasy.del('/sys_user/deleteByIds',selRows,function(res){
 				_this.userDataGrid.datagrid('clearChecked').datagrid('reload');
 			});
 		});
 		$("#edit_user").click(function(){
 			var selRows=_this.userDataGrid.datagrid('getChecked');
 			JSmile.KEasy.edit(selRows,function(){
 	 	        top.$.modalDialog({
 	                _form:"#userForm", 
 	                _datagrid:_this.userDataGrid, 	 	        	
 	 	            title : '用户信息修改',
 	 	            href:"/sys_user/edit",
 	 	            onLoad:function(){
 	 	            	var f=top.$.modalDialog.handler.find("#userForm"); 
 	 	            	f.form("reset");
 	 	            	f.form("load",selRows[0]);
 	 	            	top.$('#userName').textbox({'readonly':true}); 
 	 	            }
 	 	        }).dialog('open');
 			});
 		});
 		//搜索实现
 		$("#search_user").click(function(){
 			_this.userDataGrid.datagrid('load',$('#userSearchForm').serializeJson());
 		}); 
     },
     initDatagrid:function(){//初始化各种datagrid
    	var _this=this;
    	_this.userDataGrid=$('#user_datagrid').datagrid(JSmile.KEasy.initDatagrid({
	    	url:'/sys_user/findAllByPage',
	    	toolbar:'#user_toolbar',
	    	columns:[[
	   		 {field:'id',hidden:true},
			 {field:'ck',checkbox:true},
			 {field:'userName',title:'用户名',align:'center',width:100},
			 {field:'realName',title:'真实姓名',align:'center',width:100},
			 {field:'createDate',title:'注册时间',align:'center',width:100,sortable:true},
			 {field:'deptName',title:'部门',align:'center',width:100},
			 {field:'phone',title:'电话',align:'center',width:100},
			 {field:'email',title:'邮箱',align:'center',width:100},
			 {field:'genederName',title:'性别',align:'center',width:60},
			 {field:'lockedName',title:'是否锁定',align:'center',width:80,sortable:true},
			 {field:'remark',title:'备注',align:'center',width:100},
			 {field:'isenable',title:'是否启用',align:'center',width:80,sortable:true,formatter: function(value,row,index){
				 return JSmile.KStatus.initStatus(row.isenable);
			 }},
			 {field:'action',title:'操作',align:'center',width:100,formatter: function(value,row,index){
				 if(row.isenable=='1'){
					 return JSmile.Kit.formatString('<a style="cursor:pointer;" class="easyui-tooltip" title=\"禁用\" onclick="updateUserStatus(\'{0}\',\'{1}\');" ><i style="color:#d9534f !important" class="iconfont sn-disable"></i></a>', row.id,0);
				 }else{
					 return JSmile.Kit.formatString('<a class="easyui-tooltip" style="cursor:pointer;" title=\"启用\" onclick="updateUserStatus(\'{0}\',\'{1}\');" ><i style="color:#5cb85c !important" class="iconfont sn-enable"></i></a>', row.id,1);
				 }
			 }}
		]]
	    }));
     }
  };
  user.init();
});
/**
 * 修改密码
 */
function updateUserStatus(id,status){
	JSmile.KHttp.send({
		url:"/sys_user/update",
		data:{"id":id,"isenable":status}
	}).done(function(){
		$('#user_datagrid').datagrid('reload');
	});
}